<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link rel="stylesheet" href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css">
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/datagrid.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>

<div id="formArea">
	<table class="table-top">
		<tr>
			<td><label>所属机构</label></td>
			<td><div id="companyId"></div></td>
			<td><label>仓库名称</label></td>
			<td><input type="text" class="input" name="name"/></td>
			<td>
				<button class="button" id="query"><span>查询</span></button>
				<button class="button" id="create"><span>新增</span></button>
			</td>
		</tr>

	</table>
</div>

<div id="table"></div>

<script>
var editArea = 	['450px', '400px'];

$(function(){

    $('#companyId').search({
        name:'companyId',
        click:function(){
            app.window(layer,{
                btn:[],
                area: ['600px', '600px'],
                content: '../../common/company_tree.html?index=-1&searchId=companyId'
            });
        }
    });
    var company = {companyId:app.getCompanyId(),companyId_text:app.getCompanyName()};
    app.resetForm(company);

    $('#create').click(function(){
        app.window(layer,{
            title:'添加仓库',
            area: editArea,
            content: 'stone_add.html'
        });
    });

    $('#query').click(function(){
        $("#table").datagrid("reload",app.serializeForm());
    });

    $('#table').datagrid({
        url:app.BASE_API_URL+'store/page',
        queryParams:{companyId:app.getCompanyId()},
        columns:[
            {field:'name',title:'仓库名称',width:100},
            {field:'companyName',title:'所属机构',width:100},
            {field:'contact',title:'责任人',width:80,fixed:true},
            {field:'contactPhone',title:'联系电话',width:100},
            {field:'address',title:'地址',width:100},
            {field:'defaultStone',title:'是否默认',width:80,fixed:true,formatter:function (row) {
				if( row.defaultStone == '1' ){
					return '<span class="label-status label-success">默认仓库</span>';
				}else{
					return '';
				}
			}},
            {title:'操作',width:100,formatter:function(row){
                var edit = app.getOpt(row.id,'编辑','edit');
                var setDefault = app.getOpt(row.id,'设为默认','green');
                var del = app.getOpt(row.id,'删除','del');
                if( row.defaultStone=='1' ){
                    return edit+del+app.getHideOpt(4);
				}else{
                    return edit+del+setDefault;

				}
            }}
        ],
		loadSuccess:loadDataSuccess
    });
});

function loadDataSuccess(){
    $('.vui-opt-del').click(function(){
        var row = $(this).parent().parent().parent().data();
        layer.confirm('确定要删除选中的记录吗？', {icon: 3, title:'提示'}, function(index){
            app.request({
                url:'store/delete',
                data:{id:row.id,defaultStone:row.defaultStone},
                success:function (resp) {
                    app.actionSuccess_index(resp,index);
                }
            });

        });
	});

    $('.vui-opt-edit').click(function(){
        var row = $(this).parent().parent().parent().data();
        app.window(layer,{
            area: editArea,
            content: 'stone_add.html?id='+row.id+'&companyName='+encodeURIComponent(row.companyName)
        });
    });

    $('.vui-opt-green').click(function(){
        var row = $(this).parent().parent().parent().data();
        layer.confirm('确定把选中的仓库设置为默认仓库吗？', {icon: 3, title:'提示'}, function(index){
            app.request({
                url:'store/setDefault',
                data:{id:row.id},
                success:function(resp){
                    app.actionSuccess_index(resp,index);
                }
            });

        });
    });
}
</script>

</body>
</html>